<?php
class CmsProfileEditControl extends CmsBaseControl
{
    protected $userId;

    public function CmsProfileEditControl()
    {
        parent::__construct();
        $this->userId = $this->config->GetUserModel()->GetCurrentUserId();
    }

    public function Render()
    {
        if (RequestHandler::IsExisted('user'))
            $this->Save();

        $user = $this->config->GetUserModel()->FindById($this->userId);
        $user['profile_photo'] = Photo::GetInstance()->GetThumbUrl($user['profile_photo']);
        $this->Replace($user);
        return parent::Render();
    }

    public function Save()
    {
        try
        {
            $item = RequestHandler::Get('user');
            $item['id'] = $this->userId;
            $this->config->GetUserModel()->Save($item);
            MessageBase::Confirm('Your profile has been updated');
        }
        catch (Exception $ex)
        {
            MessageBase::Warning($ex->getMessage());
        }
    }
}
?>
